package com.jeesite.modules.task.deploy.web;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.idgen.IdGen;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.utils.excel.annotation.ExcelField;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.annotation.RepeatSubmit;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.DictUtils;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.action.entity.DcSupTaskAction;
import com.jeesite.modules.task.arrange.entity.DcSupTaskInfoArrange;
import com.jeesite.modules.task.arrange.service.DcSupTaskInfoArrangeService;
import com.jeesite.modules.task.audit.service.DcSupTaskAuditService;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfoAdd;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfoMulti;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoAddService;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoSecretaryService;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.dict.entity.DcSupTaskInfoDict;
import com.jeesite.modules.task.dict.service.DcSupTaskInfoDictService;
import com.jeesite.modules.utils.Dict;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.*;

/**
 * 任务部署-秘书一科
 * @author wk
 * @version 2022-05-09
 */
@Controller
@RequestMapping(value = "${adminPath}/taskdeploy/dcSupTaskInfoSecretary")
public class DcSupTaskInfoSecretaryController extends BaseController {

	@Autowired
	private DcSupTaskInfoService dcSupTaskInfoService;
	@Autowired
	private DcSupTaskInfoDictService dcSupTaskInfoDictService;
	@Autowired
	private DcSupTaskInfoSecretaryService dcSupTaskInfoSecretaryService;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupTaskInfo get(String id, boolean isNewRecord) {
		return dcSupTaskInfoService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表，只查询秘书一科
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "list")
	public String list(DcSupTaskInfo dcSupTaskInfo, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);
		model.addAttribute("isShow",EmpUtils.getEmployee().getEmpCode());
		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/secretary/dcSupTaskInfoGroupList";
	}

	/**
	 * 重写查询列表数据,只查询秘书一科
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "queryListData")
	@ResponseBody
	public Page<DcSupTaskInfo> queryListData(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskInfo.setPage(new Page<>(request, response));
        String[] status=dcSupTaskInfo.getTaskStatus().split(",");
        if(status.length>1){
        	dcSupTaskInfo.setTaskStatus(status[1]);
		}else {
			dcSupTaskInfo.setTaskStatus(status[0]);
		}
        if("2".equals(dcSupTaskInfo.getTaskStatus())){
			dcSupTaskInfo.setTaskStatus(null);
		}
        //我的
		if (dcSupTaskInfo.getDataLimit()!=null && dcSupTaskInfo.getDataLimit().equals("0") && EmpUtils.getEmployee().getEmpCode() !=null ){
			dcSupTaskInfo.setCreateBy(EmpUtils.getEmployee().getEmpCode());
		}
		//本科室
		if(dcSupTaskInfo.getDataLimit().equals("2")){
			dcSupTaskInfo.setCreateOrg(EmpUtils.getOffice().getOfficeCode());
		}
		//本单位
		if(dcSupTaskInfo.getDataLimit().equals("1")){
			dcSupTaskInfo.setCreateUnit(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		}

		Page<DcSupTaskInfo> page=dcSupTaskInfoSecretaryService.findLiistPage(dcSupTaskInfo);
		return page;
	}

	/**
	 * 重写查询列表数据,根据督办字号查询秘书一科任务内容
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "findListData")
	@ResponseBody
	public Page<DcSupTaskInfo> findListData(DcSupTaskInfo dcSupTaskInfo, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskInfo.setPage(new Page<>(request, response));
		String[] status=dcSupTaskInfo.getTaskStatus().split(",");
		if(status.length>1){
			dcSupTaskInfo.setTaskStatus(status[1]);
		}else {
			dcSupTaskInfo.setTaskStatus(status[0]);
		}
		if("2".equals(dcSupTaskInfo.getTaskStatus())){
			dcSupTaskInfo.setTaskStatus(null);
		}
		dcSupTaskInfo.setCreateUnit(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		Page<DcSupTaskInfo> page=dcSupTaskInfoService.findListPage(dcSupTaskInfo);
		return page;
	}

	/**
	 * 查询列表，秘书一科已提交
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "listed")
	public String secretListed(DcSupTaskInfo dcSupTaskInfo, Model model) {
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/secretary/dcSupTaskInfoGroupListed";
	}

	/**
	 * 秘书一科,根据督办字号查询任务内容,待提交页面
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "titleForm")
	public String titleFrom(DcSupTaskInfo dcSupTaskInfo, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		if("undefined".equals(dcSupTaskInfo.getMessageNumber())){
			dcSupTaskInfo.setMessageNumber(null);
		}
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/secretary/dcSupTaskInfoList";
	}

	/**
	 * 秘书一科,根据督办字号查询牵头单位,待提交页面
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "messageNumberForm")
	public String messageNumberForm(DcSupTaskInfo dcSupTaskInfo, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		if("undefined".equals(dcSupTaskInfo.getMessageNumber())){
			dcSupTaskInfo.setMessageNumber(null);
		}
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/secretary/dcSupTaskInfoTitleList";
	}

	/**
	 * 秘书一科,根据督办字号查询任务内容,已提交页面
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "messageNumberListed")
	public String messageNumberListed(DcSupTaskInfo dcSupTaskInfo, Model model) {
		model.addAttribute("dcSupTaskInfo", dcSupTaskInfo);
		// 自动获取年度，近几年前台可自由配置
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);

		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/secretary/dcSupTaskInfoListed";
	}

	/**
	 * 秘书一科,根据督办字号查询牵头单位,已提交页面
	 */
	@RequiresPermissions("taskdeploy:dcSupTaskInfoSecretary:view")
	@RequestMapping(value = "titleListed")
	public String titleListed(DcSupTaskAction dcSupTaskAction, Model model) {
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		dcSupTaskAction.setTaskId(dcSupTaskAction.getTaskId());
		model.addAttribute("dcSupTaskAction", dcSupTaskAction);
		// 自动获取年度，近几年前台可自由配置

		// 查询任务类型
		DcSupTaskInfoDict dcSupTaskInfoDict = new DcSupTaskInfoDict();
		dcSupTaskInfoDict.setDictStatus("0");
		dcSupTaskInfoDict.setOrderBy("dict_sort acs");
		List<DcSupTaskInfoDict> dicts = dcSupTaskInfoDictService.findList(dcSupTaskInfoDict);

		model.addAttribute("dicts", dicts);
		return "modules/task/deploy/secretary/dcSupTaskInfoTitleListed";
	}

	/**
	 * 重写查询列表数据,根据督办字号查询秘书一科任务内容
	 */
	@RequestMapping(value = "onePublish")
	@ResponseBody
	public Object onePublish(DcSupTaskInfo dcSupTaskInfo) {
		dcSupTaskInfo.setNormalFlag("2");
		dcSupTaskInfo.setTaskStatus("1");
		dcSupTaskInfo.setCreateUnit(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		dcSupTaskInfoService.onPublish(dcSupTaskInfo);
		return renderResult(Global.TRUE, text("一键发布成功！"));
	}

}